package com.ym.b2b.product.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ym.b2b.commons.exception.BaseException;
import com.ym.b2b.commons.service.BaseService;
import com.ym.b2b.product.dao.EmBProductPromoDAO;
import com.ym.b2b.product.dao.ProductDao;
import com.ym.b2b.product.service.EmBProductPromoService;
import com.ym.b2b.product.sqlbean.EmBProductPromo;
import com.zc.commons.base.Page;
import com.zc.commons.base.Result;
import com.zc.commons.util.ZcStringUtil;

@Service
public class EmBProductPromoServiceImpl extends BaseService implements EmBProductPromoService {

    @Autowired
    private EmBProductPromoDAO emBProductPromoDAO;

    @Autowired
    private ProductDao         productDao;

    @Override
    public Result searchPageProductSimple(EmBProductPromo emBProductPromo, Page<EmBProductPromo> page)
                                                                                                      throws BaseException {
        Result ret = new Result();
        try {
            emBProductPromoDAO.searchPageEmBProductPromo(emBProductPromo, page);
            ret.setResultPage(page);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            ret.setCodeResult(Result.CODE_100, "分页查询失败!");
        }
        return ret;
    }

    @Override
    public Result insertEmBProductPromo(EmBProductPromo record) throws BaseException {
        Result ret = new Result();
        if (ZcStringUtil.isBlank(record.getProductCode())) {
            ret.setCodeResult(Result.CODE_100, "请选择一个商品!");
            return ret;
        }
        EmBProductPromo ePromo = emBProductPromoDAO.searchEmBProductPromo(record);
        if (null != ePromo) {
            ret.setCodeResult(Result.CODE_100, "该商品已经存在促销信息中  请先删除!");
            return ret;
        }
        try {
            emBProductPromoDAO.insertEmBProductPromo(record);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            ret.setCodeResult(Result.CODE_100, "新增促销记录 sql异常!");
        }
        return ret;
    }

    @Override
    public Result updateProductPromo(EmBProductPromo record) throws BaseException {
        Result ret = new Result();
        if (ZcStringUtil.isBlank(record.getProductCode())) {
            ret.setCodeResult(Result.CODE_100, "请选择一个商品!");
            return ret;
        }
        try {
            emBProductPromoDAO.updateEmBProductPromo(record);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            ret.setCodeResult(Result.CODE_100, "新增促销记录 sql异常!");
        }
        return ret;
    }

}
